﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Globalization;
using System.Text.RegularExpressions;

namespace DigitalSolutionControls
{
    public class TextEdit:TextBox
    {
        private InputType type;
        private string numberDecimalSeparator;

        public enum InputType
        {
            NORMAL,
            NUMBER,
            DECIMAL,
            PHONE
        }

        public InputType Type
        {
            get
            {
                return type;
            }
            set
            {
                type = value;
                switch (type)
                {
                    case InputType.DECIMAL:
                    case InputType.NUMBER:
                        this.ShortcutsEnabled = false;
                        break;
                    case InputType.NORMAL:
                        this.ShortcutsEnabled = true;
                        break;
                    case InputType.PHONE:
                        this.MaxLength = 11;
                        break;
                }
            }
        }

        public TextEdit()
        {
            type = InputType.NORMAL;
            this.MinimumSize = new Size(24, 24);
            this.Font = Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.AutoSize = false;
            numberDecimalSeparator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
        }

        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            base.OnKeyPress(e);
            switch (type)
            {
                case InputType.NORMAL:
                    break;
                case InputType.DECIMAL:
                    if (!Char.IsDigit(e.KeyChar) && e.KeyChar.ToString() != numberDecimalSeparator && !Char.IsControl(e.KeyChar))
                        e.Handled = true;
                    break;
                case InputType.PHONE:
                case InputType.NUMBER:
                    if (!Char.IsNumber(e.KeyChar) && !Char.IsControl(e.KeyChar))
                        e.Handled = true;
                    break;
            }
        }

        public bool IsValidEmail()
        {
            return Regex.IsMatch(this.Text, @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z");
        }

        public bool IsValidPhone()
        {
            return this.Text.Length > 9;
        }
    }

}
